package com.viewlibrary;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

public class SignView extends View {
    private final int defaultHeight=85;

    private int signDefaultColor;
    private int signCheckedColor;
    private int signPathColor;
    private int signTextColor;
    private int signTextSize;

    private Paint defaultPaint,checkedPaint,pathPaint,textPaint;
    public SignView(Context context) {
        this(context,null);
    }

    public SignView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SignView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SignView);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = typedArray.getIndex(i);
            if (attr == R.styleable.SignView_signDefaultColor) {
                signDefaultColor=typedArray.getColor(attr, Color.GRAY);
            } else if (attr == R.styleable.SignView_signCheckedColor) {
                signCheckedColor=typedArray.getColor(attr, Color.GREEN);
            } else if (attr == R.styleable.SignView_signPathColor) {
                signPathColor=typedArray.getColor(attr, Color.WHITE);
            } else if (attr == R.styleable.SignView_signTextColor) {
                signTextColor=typedArray.getColor(attr, Color.BLACK);
            } else if (attr == R.styleable.SignView_signTextSize) {
                signTextSize=typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,15,getResources().getDisplayMetrics()));
            }
        }
        typedArray.recycle();

        defaultPaint=createPaint(signDefaultColor,0,Paint.Style.FILL,0);
        checkedPaint=createPaint(signCheckedColor,0, Paint.Style.FILL,0);
        pathPaint=createPaint(signPathColor,0, Paint.Style.STROKE,3);
        textPaint=createPaint(signTextColor,signTextSize, Paint.Style.FILL,0);
    }

    private Paint createPaint(int color, int textSize, Paint.Style style, int strokeWidth) {
        Paint paint=new Paint();
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setTextSize(textSize);
        paint.setStyle(style);
        paint.setStrokeWidth(strokeWidth);
        return paint;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode==MeasureSpec.AT_MOST){
            heightMeasureSpec=MeasureSpec.makeMeasureSpec((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,defaultHeight,getResources().getDisplayMetrics()),MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
